package com.cat.model.biz.logic.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.logic.BasLogic;
import com.cat.model.biz.logic.OrderLogic;
import com.cat.model.biz.service.bs.SmsBS;
import com.cat.model.common.enumerator.OrderStatus;
import com.cat.model.common.model.common.BasConfigVO;
import com.cat.model.common.model.common.BasOrderVO;
import com.cat.model.common.model.common.ModelInfoVO;
import com.cat.model.common.model.common.OrderNumberVO;
import com.cat.model.common.model.common.ReservationVO;
import com.cat.model.common.model.common.SolutionModelVO;
import com.cat.model.common.model.common.SolutionVO;
import com.cat.model.common.util.Constant;
import com.cat.model.common.util.LoggerManager;
import com.cat.model.common.util.OrderNumUtil;
import com.cat.model.dao.mapper.ModelMapper;

@Service("orderLogic")
public class OrderLogicImpl implements OrderLogic {

	@Autowired
	private TxCommonDAO commonDAO;
	@Autowired
	SmsBS smsBS;
	@Autowired
	ModelMapper modelMapper;
	@Autowired
	private BasLogic basLogic; 
	@Override
	public String insertOrderNumber() {
		String orderNumber = null;
		while(true){
			orderNumber = OrderNumUtil.createOrderNum();
			OrderNumberVO num = new OrderNumberVO();
			num.setOrderNumber(orderNumber);
			commonDAO.insert(num);
			break;
		}
		
		return orderNumber;
	}
	
	@Override
	public BasOrderVO getOrderByNo(String orderNum) {
		return modelMapper.getBasOrderByOrderNumber(orderNum);
	}
	
	public String updateOrderSuccess(String orderNum, String payType, String tradeNo, BigDecimal payAmount) throws SQLException {
		BasOrderVO order = getOrderByNo(orderNum);
		if(null == order){
			LoggerManager.error.error("order not exist, orderNum:"+orderNum);
			return null;
		}
		
		if(StringUtils.isNotEmpty(order.getPayType())){
			LoggerManager.error.error("order processed!, orderNum:"+orderNum);
			return null;
		}
		
		order.setStatus(OrderStatus.PAYED.getValue().toString());
		order.setPayType(payType);
		order.setPayTime(new Date());
		order.setTradeNo(tradeNo);
		
		commonDAO.updateById(order, "payType","payTime","tradeNo","status");
		
		//获取方案
		SolutionVO solution = modelMapper.getSolutionById(order.getRefBizId());
		solution.setStatus("03");//已支付
		commonDAO.updateById(solution,"status");
		List<SolutionModelVO> modelList = modelMapper.getSolutionModel(solution.getId());

		for (SolutionModelVO solutionModelVO : modelList) {
			solutionModelVO.setStatus("03");
			commonDAO.updateById(solutionModelVO, "status");
		}
		String subjectName = null;
		String  modelCode=null;
		if(modelList!=null){
			ModelInfoVO modelInfo = modelMapper.getModelInfo(modelList.get(0).getModelId());
            if(modelInfo!=null){
            	subjectName =modelInfo.getName();
            	modelCode = modelInfo.getModelCode();
            }
			if(modelList.size()>1){
				subjectName = subjectName+"等"+modelList.size()+"人";
            	modelCode = modelInfo.getModelCode()+"等"+modelList.size()+"人";

			}
		}
		ReservationVO reservation = modelMapper.getReservation(solution.getReservationId());
		reservation.setStatus("04");//已支付
		commonDAO.updateById(reservation,"status");
		
		//短信通知客服
		List<BasConfigVO> contactList =   basLogic.getBasConfigByName(Constant.CUSTOMER_SERVICE_PHONE);
		if(contactList.size()>0){
			BasConfigVO basVO = contactList.get(0);
			SimpleDateFormat time_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String time=time_sdf.format(reservation.getWorkStartTime())+"到"+time_sdf.format(reservation.getWorkEndTime());
			smsBS.sendSMSpayOk(basVO.getConfigValue(), reservation.getPhoneNo(), time, modelCode);
		}
		return "200";
	}
	
}
